package itheima.ui;

import itheima.thread.ReceiveThread;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class ClientChatFrame extends JFrame {

    private String UserName;
    public final JTextArea chatArea;
    public final JTextArea onlineUsersArea;
    private final JTextField messageField;
    private final JButton sendButton;

    public JScrollPane onlineUsersScrollPane;

    private Socket socket;
    private DataOutputStream dos;

    public ClientChatFrame(String userName, Socket socket) {
        this();
        this.UserName = userName;
        this.socket = socket;
        try {
            dos = new DataOutputStream(this.socket.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        setTitle(this.UserName + "：群聊聊天界面");

        // 初始化数据，并将socket送个一个新的线程
        new ReceiveThread(socket, this).start();
    }

    public ClientChatFrame(){
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        // 主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(Color.WHITE);

        // 聊天界面
        chatArea = new JTextArea();
        chatArea.setEditable(false);
        chatArea.setFont(new Font("楷体", Font.PLAIN, 16));
        chatArea.setBackground(new Color(240, 240, 240));
        chatArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        JScrollPane chatScrollPane = new JScrollPane(chatArea);
        chatScrollPane.setBorder(BorderFactory.createTitledBorder("聊天记录"));
        mainPanel.add(chatScrollPane, BorderLayout.CENTER);

        // 在线人数展示
        onlineUsersArea = new JTextArea();
        onlineUsersArea.setEditable(false);
        onlineUsersArea.setFont(new Font("楷体", Font.PLAIN, 16));
        onlineUsersArea.setText("");
        onlineUsersArea.setBackground(new Color(240, 240, 240));
        onlineUsersArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        onlineUsersScrollPane = new JScrollPane(onlineUsersArea);
        onlineUsersScrollPane.setBorder(BorderFactory.createTitledBorder("在线人数：0"));
        onlineUsersScrollPane.setPreferredSize(new Dimension(130, 0)); // 设置首选宽度
        mainPanel.add(onlineUsersScrollPane, BorderLayout.EAST);

        // 消息输入框和发送按钮
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new BorderLayout());
        inputPanel.setBackground(Color.WHITE);
        inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        messageField = new JTextField();
        messageField.setFont(new Font("楷体", Font.PLAIN, 16));
        messageField.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        inputPanel.add(messageField, BorderLayout.CENTER);

        sendButton = new JButton("发送");
        sendButton.setFont(new Font("楷体", Font.BOLD, 16));
        sendButton.setBackground(new Color(0, 123, 255));
        sendButton.setForeground(Color.WHITE);
        sendButton.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
        inputPanel.add(sendButton, BorderLayout.EAST);

        mainPanel.add(inputPanel, BorderLayout.SOUTH);

        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String message = messageField.getText();
                try{
                    dos.writeInt(2);
                    dos.writeUTF(message);
                    dos.flush();
                    messageField.setText("");

                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        add(mainPanel);
        setVisible(true);
    }

    // 当拿到socket传入的数据时更新在线用户列表

}
